<?php 
require_once dirname(dirname(__FILE__)) .'/cse-init.php';

// authentication is required!
Cinnamon::registry('session')->applyAuthenticationPolicy();

// load required classes
Cinnamon::loadClass('Container');
Cinnamon::loadClass('DataGrid');
Cinnamon::loadClass('ArrayCollection');
Cinnamon::loadClass('DatabaseCollection');

// fetch required object from registry
$db 	= Cinnamon::registry('db');
$view 	= Cinnamon::registry('view');

//
// Summary datagrid
//
$summary = new Container('Summary');
$dg = new Datagrid();
$summary->addChild($dg);
$dg->headers = false;
$dg->addColumn('', 'field', 'labela');
$dg->addColumn('', 'field', 'valuea');
$dg->addColumn('', 'field', 'labelb');
$dg->addColumn('', 'field', 'valueb');

$item = $db->requests(
			'MIN(request_datetime) AS request_datetime, '.
			'COUNT(DISTINCT request_id) AS views, '.
			'COUNT(DISTINCT visit_id) AS visits'
		)->getItemAt(0);

$onlineVisitors = $db->requests('COUNT(DISTINCT visit_id)')
		->filter('request_utc_datetime > \''. $db->getDatetime(time() - ( 15 *  SECS_PER_MIN )) .'\'')
		->getValueAt(0, 0);

$lastVisitor = $db->requests('MAX(request_datetime)')->getValueAt(0, 0);

$dg->setDataProvider( new ArrayCollection( array(
	array(
		'labela'=>'Since', 'valuea'=>$item->request_datetime
	), array(
		'labela'=>'Visits', 'valuea'=>$item->visits, 
		'labelb'=>'Page views', 'valueb'=>$item->views
	), array(
		'labela'=>'Online visitors', 'valuea'=>$onlineVisitors .' (<small><a href="'. CSEURL .'/reports/spy.php">Spy on them</a></small>)', 
		'labelb'=>'Last visit', 'valueb'=>$lastVisitor
	)
) ) );

//
// History datagrid
//
Cinnamon::loadClass('Container');
$history = new Container('History');

// History > Since Fragment
$fragment = new DataGrid('Since');
$fragment->addColumn('Since', 'field', 'visit_datetime', 'format', 'month');
$fragment->addColumn('Visits', 'field', 'unique_visits');
$fragment->addColumn('Page views', 'field', 'total_visits');
$fragment->addColumn('Pages per visit', 'field', 'pages_per_visit');

$dbcol = $db->requests( 
			'MIN(request_datetime) AS visit_datetime, '.
			'COUNT(request_id) AS total_visits, '.
	  		'COUNT(DISTINCT visit_id) AS unique_visits, '.
			'COUNT(DISTINCT request_url) AS pages_viewed, '.
			'(COUNT(request_url)/COUNT(DISTINCT visit_id)) AS pages_per_visit '
		);
		
$fragment->setDataProvider($dbcol);
$history->addChild($fragment);

// History > Past week
$fragment = clone $fragment;
$fragment->removeColumnAt(0);
$fragment->insertColumnAt(0, 'Past Week', 'field', 'request_datetime', 'format', 'day');

$dbcol = $db->requests(
			'request_datetime, '.
			'COUNT(request_id) AS total_visits, '.
	  		'COUNT(DISTINCT visit_id) AS unique_visits, '.
			'COUNT(DISTINCT request_url) AS pages_viewed, '.
			'(COUNT(request_url)/COUNT(DISTINCT visit_id)) AS pages_per_visit '
		)->filter(
			"DATE(request_datetime) BETWEEN '". $db->getDate(time() - (6 * SECS_PER_DAY)) ."' AND '". $db->getDate() ."'"
		)->group('DATE(request_datetime)')->sort('request_datetime', 'DESC');

$fragment->setDataProvider($dbcol);
$history->addChild($fragment);

// History > Past year
$fragment = clone $fragment;
$fragment->removeColumnAt(0);
$fragment->insertColumnAt(0, 'Past Year', 'field', 'request_datetime', 'format', 'month');

$dbcol = $db->requests( 
			'request_datetime, '.
			'COUNT(request_id) AS total_visits, '.
		  	'COUNT(DISTINCT visit_id) AS unique_visits, '.
			'COUNT(DISTINCT request_url) AS pages_viewed, '.
			'(COUNT(request_url)/COUNT(DISTINCT visit_id)) AS pages_per_visit '
		)->group('YEAR(request_datetime), MONTH(request_datetime)')
		->sort('YEAR(request_datetime)', 'DESC')->sort( 'MONTH(request_datetime)', 'DESC')
		->slice(0, 12);
		
$fragment->setDataProvider($dbcol);
$history->addChild($fragment);

// Render output
$view->addChild($summary);
$view->addChild($history);
$view->render();
?>